package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.config.NoLogin;
import com.pojo.RechargeStation;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statistical")
public class StatisticalAction {

    @Autowired
    private QiyeService qiyeService;
    @Autowired
    private WuyeService wuyeService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private ChongzhiService czService;
    @Autowired
    private XiaofeiService xfService;

    @Autowired
    private RechargeStationService rechargeStationService;

    /**
     * 统计企业总数、物业总数、员工总数、疫苗接种人数、核酸检测人数、阳性核酸检测人数
     */
    @RequestMapping("/getCount")
    public ResponseBean getCount() {
        Map resultMap = new HashMap();
        long qiyeCount = qiyeService.count();
        long wuyeCount = wuyeService.count();
        long staffCount = staffService.count();
        resultMap.put("qiyeCount",qiyeCount);
        resultMap.put("wuyeCount",wuyeCount);
        resultMap.put("staffCount",staffCount);
        return new ResponseBean(1,1,"获取成功",resultMap);
    }

    /**
     * 获取充值和消费总和
     */
    @RequestMapping("/getCzXfTotal")
    public ResponseBean getCzXfTotal() {
        Map resultMap = new HashMap();
        BigDecimal czTotalAmount = czService.getTotalAmount();
        BigDecimal xfTotalAmount = xfService.getTotalAmount();
        resultMap.put("czTotalAmount",czTotalAmount);
        resultMap.put("xfTotalAmount",xfTotalAmount);
        return new ResponseBean(1,1,"获取成功",resultMap);
    }

    /**
     * 获取每个国籍人数
     */
    @RequestMapping("/getGuojiCount")
    public ResponseBean getGuojiCount() {
        List<Map<String, Long>> resultMap = staffService.getGuojiCount();
        //Gson gson=new Gson();
        //String mapJson = gson.toJson(resultMap);
        Map m = new HashMap();
        m.put("lists" , resultMap);
        return new ResponseBean(1,"获取成功", m);
    }

    /**
     * 获取本月充值和消费数据
     */
    @RequestMapping("/getDayAmountByMonth")
    public ResponseBean getDayAmountByMonth() {
        Map resultMap = new HashMap();
        List<Map<String,Object>> czList = czService.getDayAmountByMonth();
        List<Map<String,Object>> xfList = xfService.getDayAmountByMonth();
        SimpleDateFormat year = new SimpleDateFormat("yyyy");
        SimpleDateFormat month = new SimpleDateFormat("MM");
        String date = xfList.get(0).get("day").toString();
        resultMap.put("getYear",year.format(new Date()));
        resultMap.put("getMonth",month.format(new Date()));

        resultMap.put("cz",czList);
        resultMap.put("xf",xfList);
        return new ResponseBean(1,1,"获取成功",resultMap);
    }



    /**
     * 统计充值点的充值金额，包含昨天，今天，累计三种类型
     * 仅适用于充值点操作端
     * @return
     */
    @RequestMapping("/station/countTotal")
    public ResponseBean countTotal(HttpServletRequest request){
        try {
            RechargeStation rechargeStation = (RechargeStation) request.getSession().getAttribute("rechargeStation");
            if (null == rechargeStation) {
                return new ResponseBean(500, "请重新登录", null);
            }
            SimpleDateFormat year = new SimpleDateFormat("yyyy");
            SimpleDateFormat month = new SimpleDateFormat("MM");
            String monthStart = year.format(new Date())+"-"+month.format(new Date())+"-01 00:00:00";
            String monthEnd = year.format(new Date())+"-"+month.format(new Date())+"-31 23:59:59";
            Map data = new HashMap();
            data.put("todayAmount",0);
            data.put("monthAmount",0);
            data.put("totalAmount",0);
            Map result = czService.countMoney(rechargeStation.getId(),monthStart,monthEnd);
            if(result!=null){
                if(result.containsKey("todayAmount")){
                    data.put("todayAmount",result.get("todayAmount"));
                }
                if(result.containsKey("monthAmount")){
                    data.put("monthAmount",result.get("monthAmount"));
                }
                if(result.containsKey("totalAmount")){
                    data.put("totalAmount",result.get("totalAmount"));
                }
            }

            return new ResponseBean(1, "成功", data);
        }catch (Exception e){
            return new ResponseBean(500, "系统异常", null);
        }
    }


    @RequestMapping("/station/getDayAmountByMonth")
    public ResponseBean getDayAmountByMonth(HttpServletRequest request) {
        try {
            RechargeStation rechargeStation = (RechargeStation) request.getSession().getAttribute("rechargeStation");
            if (null == rechargeStation) {
                return new ResponseBean(500, "请重新登录", null);
            }
            Map resultMap = new HashMap();
            List<Map<String, Object>> czList = czService.getDayAmountOfMonthByStation(rechargeStation.getId());
            SimpleDateFormat year = new SimpleDateFormat("yyyy");
            SimpleDateFormat month = new SimpleDateFormat("MM");
            resultMap.put("getYear", year.format(new Date()));
            resultMap.put("getMonth", month.format(new Date()));

            resultMap.put("cz", czList);
            return new ResponseBean(1, 1, "获取成功", resultMap);

        }catch (Exception e){
            System.out.print(e);
            return new ResponseBean(500, "系统异常", null);
        }
    }


    /**
     * 统计充值点总数、累计充值金额、今日充值金额、本月充值金额
     */
    @RequestMapping("/getRechargeCount")
    public ResponseBean getRechargeCount() {
        Map resultMap = new HashMap();
        long rechargeStationCount = rechargeStationService.getRechargeCount();
        BigDecimal rechargeStationTotalAmount = czService.getRechargeTotalAmount();
        BigDecimal rechargeStationDayAmount = czService.getRechargeDayAmount();
        BigDecimal rechargeStationMonthAmount = czService.getRechargeMonthAmount();
        resultMap.put("rechargeCount",rechargeStationCount);
        resultMap.put("totalAmount",rechargeStationTotalAmount);
        resultMap.put("dayAmount",rechargeStationDayAmount);
        resultMap.put("monthAmount",rechargeStationMonthAmount);
        return new ResponseBean(1,1,"获取成功",resultMap);
    }

    /**
     * 获取每个充值点的充值总额
     */
    @RequestMapping("/getRechargeAmount")
    public ResponseBean getRechargeAmount() {
        Map resultMap = new HashMap();
        List<Map<String,Object>> rechargeStationList = czService.getRechargeAmount();
        resultMap.put("rechargeStationList",rechargeStationList);
        return new ResponseBean(1,1,"获取成功",resultMap);
    }

    /**
     * 统计本年度每月充值点充值额
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getCurrentRechargeAmount")
    public ResponseBean getCurYearRechargeAmount(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "0") int limit) {
        IPage rechargeStationList = czService.getCurYearRechargeAmount(page, limit);
        return new ResponseBean(0,rechargeStationList.getTotal(),"获取成功",rechargeStationList.getRecords());
    }

}
